import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/user_center_page/view/user_work_list_view.dart';
import 'package:tiktokadult/hj_page/user_center_page/widget/user_center_header.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/global_store/store.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/widget/follow_button.dart';

import '../../tools_base/indicator/custom_tab_indicator.dart';
import '../../tools_base/widget/net_image_widget.dart';
import 'user_center_logic.dart';

//用户首页
class UserCenterPage extends StatelessWidget {
  static const routeName = '/usercenter';

  const UserCenterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: UserCenterController(),
      tag: "${DateTime.now().millisecondsSinceEpoch}",
      builder: (logic) => Scaffold(
        extendBodyBehindAppBar: true,
        body: _buildContent(logic),
      ),
    );
  }

  Widget _buildContent(UserCenterController logic) {
    if (logic.model == null) return LoadingCenterWidget();
    if (logic.expandedHeight == null) {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        logic.calculateHeaderHeight();
      });
      return Container(
        alignment: Alignment.topCenter,
        child: UserCenterHeader(key: logic.globalKey, controller: logic),
      );
    }
    return ExtendedNestedScrollView(
        controller: logic.scrollCtr,
        headerSliverBuilder: (ctx, value) {
          return [
            SliverAppBar(
              expandedHeight: logic.expandedHeight,
              centerTitle: false,
              titleSpacing: 0,
              title: _buildAppBarTitle(logic),
              leading: InkWell(
                onTap: () => Get.back(),
                child: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                ),
              ),
              backgroundColor: Color(0xff050f17),
              pinned: true,
              floating: false,
              elevation: 0,
              // excludeHeaderSemantics: true,
              flexibleSpace: FlexibleSpaceBar(
                collapseMode: CollapseMode.pin,
                background: UserCenterHeader(controller: logic),
              ),
              bottom: PreferredSize(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  width: double.infinity,
                  height: 44,
                  child: TabBar(
                    tabAlignment: TabAlignment.fill,
                    tabs: logic.titleArr.map((e) {
                      return Container(
                        alignment: Alignment.center,
                        child: Text(e),
                      );
                    }).toList(),
                    labelStyle: TextStyle(fontSize: 14),
                    labelColor: Color(0xE5FFFFFF),
                    unselectedLabelStyle: TextStyle(fontSize: 14),
                    unselectedLabelColor: Color(0x73FFFFFF),
                    controller: logic.mainTabCtr,
                    padding: EdgeInsets.zero,
                    isScrollable: false,
                    labelPadding: EdgeInsets.zero,
                    indicator: CustomIndicator(
                      height: 4,
                      width: 16,
                      useGradient: true,
                      gradientColors: [
                        Color(0x00E1351F),
                        Color(0xFFE1351F),
                      ],
                      offsetY: 4,
                    ),
                  ),
                ),
                preferredSize: Size.fromHeight(44),
              ),
            ),
          ];
        },
        onlyOneScrollInBody: true,
        physics: BouncingScrollPhysics(),
        body: TabBarView(
          controller: logic.mainTabCtr,
          children: List.generate(
            logic.titleArr.length,
            (index) {
              return UserWorkListView(logic.model?.uid ?? 0, logic.titleParam[index]);
            },
          ),
        ));
  }

  Widget _buildAppBarTitle(UserCenterController logic) {
    return Obx(() {
      if (logic.isShowAppMenu.value) {
        return Container(
          child: Row(
            children: [
              NetworkImageLoader(
                imageUrl: logic.model?.portrait ?? '',
                width: 38,
                height: 38,
                borderRadius: 38,
              ),
              12.sizeBoxW,
              Column(
                children: [
                  Text(
                    "${logic.model?.name ?? ""}",
                    style: const TextStyle(
                      fontSize: 14,
                      color: Color(0xffffffff),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  Visibility(
                    visible: logic.model?.vipName != null && logic.model?.vipName?.isNotEmpty == true,
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 6, vertical: 1),
                      margin: EdgeInsets.only(right: 4, bottom: 3),
                      decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(6))),
                      child: Text(
                        '${logic.model?.vipName}',
                        style: TextStyle(
                          color: Color(0xff212121),
                          fontSize: 10,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              Spacer(),
              if (!globalStore.isMe(logic.model?.uid))
                FollowButton(
                  mediaId: (logic.model?.uid ?? 0).toString(),
                  isFollow: logic.model?.isFollow,
                  followType: FollowEnum.user,
                  successsAction: (isSuccess) => logic.model?.isFollow = isSuccess,
                ),
              16.sizeBoxW,
            ],
          ),
        );
      }
      return SizedBox();
    });
  }
}
